"""
在Python2中由urllib和urllib2两个库来实现请求发送，而在Python3中将这两个库合并到了一起，统一为urllib
request → 打开和浏览url中内容；
error → 包含从urllib.request发生的错误或异常；
parse → 解析url；
robotparser → 解析robots.txt文件；
"""

from urllib import request, parse
import json
# 1.基本请求
resp = request.urlopen('https://www.baidu.com')
print("resp.geturl：", resp.geturl())
print("resp.msg：", resp.msg)
print("resp.status：", resp.status)
print("resp.version：", resp.version)
print("resp.reason：", resp.reason)
print("resp.debuglevel：", resp.debuglevel)
print("resp.getheaders：", resp.getheaders()[0:2])
print(resp.read().decode('utf-8'))

# 2.中文编码
chinese_url = 'https://search.jd.com/Search?keyword='+parse.quote("爬虫")
print(chinese_url)

chinese_url = parse.unquote(chinese_url)
print(chinese_url)

# 3.下载文件 (下载路径默认 当前路径)
# 方法1
img_test = "https://ss0.baidu.com/7Po3dSag_xI4khGko9WTAnF6hhy/zhidao/pic/item/9c16fdfaaf51f3de9ba8ee1194eef01f3a2979a8.jpg"
imgRequest = request.urlopen(img_test)
f = open("../base_request_test.png", "wb")
f.write(imgRequest.read())
# with open("base_request_test.png", "wb") as f:
#     f.write(imgRequest.read())

# 方法2
request.urlretrieve(img_test, "../base_request_test1.jpg")


def _get_request():
    base_url = "https://www.wanandroid.com"
    get_url = base_url + "/banner/json"
    get_resp = request.urlopen(get_url)
    get_result = json.loads(get_resp.read().decode('utf-8'))
    get_result_format = json.dumps(get_result, indent=2, sort_keys=True, ensure_ascii=False)
    print(get_result_format)

def _post_request():
    base_url = "https://www.wanandroid.com"
    post_url = base_url+'/article/query/0/json'
    values = {'k': "爬虫"}
    # 4.1 数据 请求数据编码
    data = parse.urlencode(values).encode(encoding="utf-8")
    post_request = request.Request(post_url, data=data)
    # 4.2 响应数据解码
    post_respon = request.urlopen(post_request)
    post_result = json.loads(post_respon.read().decode('utf-8'))  #post_result 格式为字典格式 ，要解码
    # 4.3 格式化输出（字典 转 json）
    resustl_format = json.dumps(post_result, indent=2, sort_keys=True, ensure_ascii=False)
    print(resustl_format)

# 4.模拟Get请求
# _get_request()

# 5.模拟post请求
_post_request()